var express = require('express');
var fs = require('fs');
var mysql = require('mysql');
var ejs = require('ejs');

var router = express.Router();

//连接MySQL数据库
var client = mysql.createConnection({
    host: "127.0.0.1",
    port:"3306",
    user: 'root',
    password: '123456',
    database: 'wrh'
});
//判断数据库是否连接成功
client.connect(function(err){
    if(err){
         console.log('[query] - :'+err);
         return;
    }
    console.log('[connection connect]  MySQL数据库连接成功!');
});


/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index');
  });
  
  router.post('/main', function(req, res, next) {
  
    //获取到用户输入的内容
  var val = req.body;
  var username = val.username;
  var password = val.password;
  //查询数据
  client.query('select * from user where username = ? and password = ?',[username,password],function(err,data){
        if( err ){
          throw err;
        }else if( data.length > 0 ){
  
          res.render('main');
  
        }else{
          res.write('<head><meta charset="utf-8"/></head>');
          res.end('用户名或密码有误~!');
  
        }
  
    })
  
  });






module.exports = router;

